package com.fasteat.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.Orderform;
import com.fasteat.mall.entity.vo.OrderformProductVo;
import com.fasteat.mall.entity.vo.NewOrderformVo;
import com.fasteat.mall.service.OrderformService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-01-19
 */
@RestController
@CrossOrigin
@Api(description = "订单接口")
@RequestMapping("/mall/orderform")
public class OrderformController {

    @Autowired
    private OrderformService orderformService;

    /**
     * 按用户分页查看订单
     *
     * @return
     */
    @GetMapping("/acceptProductByUserId/{size}/{current}/{userId}")
    @ApiOperation("按用户分页查看订单")
    public Result acceptProductByUserId(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current,
                                        @PathVariable(name = "userId") @ApiParam(required = true, value = "当前页码") String userId) {
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<Orderform> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        orderformService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 新增订单
     *
     * @param orderformVo
     * @return
     */
    @ApiOperation(value = "下单并刷新库存销量")
    @PostMapping
    public Result save(@RequestBody NewOrderformVo orderformVo) {
        return orderformService.CustomerBuy(orderformVo) ?
                Result.success("添加成功")
                :
                Result.error("添加失败");
    }

    /**
     * 根据id删除订单
     * @param orderFormId
     * @return
     */
    @DeleteMapping(value = "/delete/{orderFormId}")
    @ApiOperation(value = "根据订单id删除订单")
    public Result deleteById(@PathVariable(name = "orderFormId", required = true)
                             @ApiParam(value = "orderFormId", required = true)
                                     String orderFormId) {
        if (orderFormId == null) {
            return Result.error("删除失败");
        }
        boolean result = orderformService.removeById(orderFormId);
        return result ?
                Result.success("删除成功")
                :
                Result.error("删除失败");
    }

    /**
     * 修改订单信息
     * @param orderform
     * @return
     */
    @ApiOperation(value = "根据订单id修改订单信息")
    @PutMapping
    public Result updateProduct(@RequestBody
                                @ApiParam(value = "订单OrderForm对象", required = true)
                                        Orderform orderform) {
        if (StringUtils.isEmpty(orderform.getId())) {
            return Result.error("修改失败");
        }
        orderform.setAftersalesStatus(0);
        boolean result = orderformService.updateById(orderform);
        return result
                ?
                Result.success("修改成功")
                :
                Result.error("修改失败");
    }

    /**
     * 确认收货
     * @param orderFormId
     * @return
     */
    @ApiOperation(value = "确认收货")
    @PutMapping("/receiveGood/{orderFormId}")
    public Result receiveGood(@PathVariable(name = "orderFormId", required = true)
                                  @ApiParam(value = "orderFormId", required = true)
                                          String orderFormId) {
        if (StringUtils.isEmpty(orderFormId)) {
            return Result.error("订单号为空");
        }
        Orderform orderform = orderformService.getById(orderFormId);
        orderform.setOrderStatus(4);
        boolean result = orderformService.updateById(orderform);
        return result
                ?
                Result.success("修改成功")
                :
                Result.error("修改失败");
    }

    /**
     * 查看所有订单
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("获取所有订单")
    public Result selectProductList() {
        return Result.success().data(orderformService.list(null));
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/{size}/{current}")
    @ApiOperation("分页查看订单")
    public Result acceptProduct(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<Orderform> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
        orderformService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/{orderformStatus}/{size}/{current}/{userId}")
    @ApiOperation("按用户按类别分页查看订单")
    public Result selectProductByStatus(@PathVariable(name = "orderformStatus") @ApiParam(required = true, value = "订单状态") Integer orderformStatus,
                                        @PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                        @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current,
                                        @PathVariable(name = "userId") @ApiParam(required = true, value = "当前页码") String userId) {
//        if(orderformStatus != 1 ||
//                orderformStatus != 2 ||
//                orderformStatus != 4 ||
//                orderformStatus != 3 ||
//                orderformStatus != 5) {
//            return Result.error("查询订单类型不存在。请输入1、2、4、5");
//        }
        if(userId.equals(null)) {
            return Result.error("请输入用户id");
        }
        if (!orderformStatus.equals(1)
                && !orderformStatus.equals(2)
                && !orderformStatus.equals(3)
                && !orderformStatus.equals(4)
                && !orderformStatus.equals(5)
        ) {
            return Result.error("请输入正确的类型");
        }
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<Orderform> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        wrapper.eq("order_status", orderformStatus);
        orderformService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/vo/{orderformStatus}/{start}/{end}/{userId}")
    @ApiOperation("按用户按类别分页查看订单")
    public Result selectByStatus(@PathVariable(name = "orderformStatus") @ApiParam(required = true, value = "订单状态") Integer orderformStatus,
                                        @PathVariable(name = "start") @ApiParam(required = true, value = "尺寸") int start,
                                        @PathVariable(name = "end") @ApiParam(required = true, value = "当前页码") int end,
                                        @PathVariable(name = "userId") @ApiParam(required = true, value = "当前页码") String userId) {
        if(userId.equals(null)) {
            return Result.error("请输入用户id");
        }
        if (!orderformStatus.equals(1)
                && !orderformStatus.equals(2)
                && !orderformStatus.equals(3)
                && !orderformStatus.equals(4)
                && !orderformStatus.equals(5)
        ) {
            return Result.error("请输入正确的类型");
        }
        List<OrderformProductVo> orderformProductVos = orderformService.pageVoIAmAngry(start, end, orderformStatus, userId);
        return Result.success().data(orderformProductVos);
    }

    /**
     * 查询用户对应订单状态订单数量
     *
     * @return
     */
    @GetMapping("/getCount")
    @ApiOperation("查询用户对应订单状态订单数量")
    public Result getUserOrderformCount(Integer orderformStatus, String userId) {
        if(userId.equals(null)) {
            return Result.error("请输入用户id");
        }
        if (!orderformStatus.equals(1)
                && !orderformStatus.equals(2)
                && !orderformStatus.equals(3)
                && !orderformStatus.equals(4)
                && !orderformStatus.equals(5)
        ) {
            return Result.error("请输入正确的类型");
        }
        QueryWrapper<Orderform> wrapper = new QueryWrapper<Orderform>();
        wrapper.eq("order_status", orderformStatus);
        wrapper.eq("user_id", userId);
        int count = orderformService.count(wrapper);
        return Result.success().data(count);
    }

    /**
     * 分页查看订单
     *
     * @return
     */
    @GetMapping("/selectProductByAfterSalesStatus/{size}/{current}/{userId}")
    @ApiOperation("按用户按分页查看订单申请售后订单")
    public Result selectProductByAfterSalesStatus(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
                                        @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current,
                                        @PathVariable(name = "userId") @ApiParam(required = true, value = "当前页码") String userId) {
        size = size <= 0 ? 5 : size;
        current = current <= 0 ? 1 : current;
        Page<Orderform> page = new Page(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        wrapper.ne("aftersales_status", 0);
        orderformService.page(page, wrapper);
        return Result.success().data(page);
    }

    /**
     * 通过订单ID获取订单信息
     *
     * @param orderFormId
     * @return
     */
    @GetMapping("findById/{orderFormId}")
    @ApiOperation("通过订单ID获取订单信息")
    public Result findById(@PathVariable(name = "orderFormId", required = true)
                               @ApiParam(value = "订单ID", required = true)
                                       String orderFormId) {
        if (StringUtils.isEmpty(orderFormId)) {
            return Result.error("查询失败");
        }
        // 从数据库中查询商品的信息
        Orderform orderform = orderformService.getById(orderFormId);
        return Result.success("查询成功").data(orderform);
    }

//    /**
//     * 退款
//     *
//     * @param aftersalesDto
//     * @return
//     */
//    @ApiOperation(value = "申请退款接口")
//    @PutMapping("/refund")
//    public Result refundApi(@RequestBody AftersalesDto aftersalesDto) {
//        orderformService.refund(aftersalesDto);
//        return Result.success();
//    }
//
//    /**
//     * 退货
//     *
//     * @param aftersalesDto
//     * @return
//     */
//    @ApiOperation(value = "申请退货接口")
//    @PutMapping("/returnGoods")
//    public Result returnGoodsApi(@RequestBody AftersalesDto aftersalesDto) {
//        orderformService.returnGoods(aftersalesDto);
//        return Result.success();
//    }
//
//    /**
//     * 换货
//     *
//     * @param aftersalesDto
//     * @return
//     */
//    @ApiOperation(value = "申请换货接口")
//    @PutMapping("/changeGoods")
//    public Result changeGoods(@RequestBody AftersalesDto aftersalesDto) {
//        orderformService.returnGoods(aftersalesDto);
//        return Result.success();
//    }
//
////    //查看申请退款
////    @ApiOperation(value = "查看申请退款接口")
////    @GetMapping("/selectRefundPage/{size}/{current}")
////    public Result selectRefundPage(@PathVariable(name = "size") @ApiParam(required = true, value = "尺寸") int size,
////                                @PathVariable(name = "current") @ApiParam(required = true, value = "当前页码") int current) {
////        size = size <= 0 ? 5 : size;
////        current = current <= 0 ? 1 : current;
////        Page<AftersalesDto> page = new Page(current, size);
////        afterSalesService.page(page, orderformService.queryRefund());
////        return Result.success().data(page);
////    }
//
//    //查看申请退款
//    @ApiOperation(value = "查看申请退款接口")
//    @GetMapping("/selectRefund")
//    public Result selectRefund() {
//        return Result.success("查询成功").data(orderformService.queryRefund());
//    }
//
//    //查看申请退款
//    @ApiOperation(value = "查看申请退货接口")
//    @GetMapping("/selectReturn")
//    public Result selectReturn() {
//        return Result.success("查询成功").data(orderformService.queryReturnGoods());
//    }
//
//    //查看申请换货
//    @ApiOperation(value = "查看申请换货接口")
//    @GetMapping("/selectExchange")
//    public Result selectExchange() {
//        return Result.success("查询成功").data(orderformService.queryExchangeGoods());
//    }

}

